package com.huobao.yunying.activitys;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Dingdan_Detail;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.DingdanDetail;
import com.huobao.yunying.entity.DingdanDetail_good_list;
import com.huobao.yunying.entity.DingdanDetail_orderextm;
import com.huobao.yunying.entity.Gouwuche_spec;
import com.huobao.yunying.entity.Gouwuche_specification;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Act_Dingdan_detail extends HuobaoBaseActivity implements OnClickListener {
	private TextView tv_bianhao;
	 private TextView tv_zongjia;
	private TextView tv_time;
	private TextView tv_status;
	private TextView btn_fukuan;
	private TextView tv_storename;
	private TextView tv_mai_xingming;
	private TextView tv_tel;
	private TextView tv_name;
	private TextView tv_dianhua;
	private TextView tv_address;
	private TextView tv_dazhe;
	private TextView tv_yunfei;
	private TextView tv_peisong;
	private String order_id;
	private TextView tv_jine;//应付金额

	
	@ViewInject(R.id.listview)
	private NoScrollListView listview;
	private Adapter_Dingdan_Detail adapter;
	

	private DingdanDetail detail;

	protected void show(DingdanDetail detail) {
		this.detail = detail;
//		if (detail.good_list != null && detail.good_list.size() != 0)
//			showGuige(llyt_guige, detail.good_list.get(0).specification);
		tv_bianhao.setText("订单编号：" + detail.order_sn);
		 tv_zongjia.setText("¥" + detail.order_amount);
		tv_time.setText("订单日期：" + detail.add_time);
		tv_status.setText("订单状态：" + detail.status_text);
		if (detail.status.equals("11")) {
			btn_fukuan.setVisibility(View.VISIBLE);
		} else {
			btn_fukuan.setVisibility(View.GONE);
		}
		tv_storename.setText("店铺名称：" + detail.store_name);
		tv_mai_xingming.setText("卖家姓名：" + detail.owner_name);
		tv_tel.setText("联系电话：" + detail.tel);
		
		tv_jine.setText("¥" + detail.order_amount);
		DingdanDetail_orderextm orderextm = detail.orderextm;
		tv_name.setText(orderextm.consignee);
		tv_dianhua.setText(orderextm.phone_mob);
		tv_address.setText(orderextm.region_name + " " + orderextm.address);
		if(detail.discount == null || detail.discount.equals("null") || detail.discount.equals("")){
			tv_dazhe.setText("￥0.00");
		}else{
			tv_dazhe.setText("￥" + detail.discount);
		}
		tv_yunfei.setText("￥" + orderextm.shipping_fee);
		tv_peisong.setText(orderextm.shipping_name);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_fukuan:
			// SkipUtils.Pay(context, detail.order_sn, detail.order_amount,
			// detail.store_name+"商品");
			Map<String, String> map = HttpUtil.getParams("app", "cashier", "order_id", "[" + detail.order_id + "]");
			Http.getInstant().sendRequest(context, map, new HttpListener() {

				@Override
				public void onHttpSuccess(String result) {
					JSONObject jsonObject = JSON.parseObject(result);
					jsonObject = jsonObject.getJSONObject("retval");
					String order_amount = jsonObject.getString("order_amount");
					String order_sn = jsonObject.getString("order_sn");
					SkipUtils.Pay(context, order_sn, order_amount, detail.good_list.get(0).goods_name);
				}

				@Override
				public void onHttpError(String error) {

				}
			});
			break;
		case R.id.llyt:
			SkipUtils.seeDetail(context, detail.good_list.get(0).goods_id, detail.good_list.get(0).goods_image);
			break;
		}
	}

	@Override
	protected int getContentView() {
		return R.layout.layout_dingdan_detail;
	}

	@Override
	protected String getTitleText() {
		return "订单详情";
	}

	@Override
	protected void init() {
		addRightView();
		order_id = getIntent().getStringExtra("order_id");
		adapter = new Adapter_Dingdan_Detail(context);
		listview.setAdapter(adapter);
		initView();
		getData();
	}

	private void addRightView() {
		LinearLayout layout = new LinearLayout(this);
		TextView tv = new TextView(this);
		tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
		tv.setText("客服");
		tv.setBackgroundResource(R.drawable.selector_commect_text_btn);
		tv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_service_blue,0, 0, 0);
		tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("dingdan", detail);
				SkipUtils.toChat(Act_Dingdan_detail.this, bundle);
			}
		});
		layout.addView(tv);
		mAbTitleBar.addRightView(layout);
	}

	private void initView() {
		tv_jine = (TextView) findViewById(R.id.tv_jine);
		tv_bianhao = (TextView) findViewById(R.id.tv_bianhao);
		 tv_zongjia = (TextView) findViewById(R.id.tv_zongjia);
		tv_time = (TextView) findViewById(R.id.tv_time);
		tv_status = (TextView) findViewById(R.id.tv_status);
		btn_fukuan = (TextView) findViewById(R.id.btn_fukuan);
		tv_storename = (TextView) findViewById(R.id.tv_storename);
		tv_mai_xingming = (TextView) findViewById(R.id.tv_mai_xingming);
		tv_tel = (TextView) findViewById(R.id.tv_tel);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_dianhua = (TextView) findViewById(R.id.tv_dianhua);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_dazhe = (TextView) findViewById(R.id.tv_dazhe);
		tv_yunfei = (TextView) findViewById(R.id.tv_yunfei);
		tv_peisong = (TextView) findViewById(R.id.tv_peisong);

		btn_fukuan.setOnClickListener(this);
	}

	private void getData() {
		Map<String, String> map = HttpUtil.getParams("app", "buyerorder", "act", "view", "order_id", order_id);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONObject object = jsonObject.getJSONObject("retval");
				detail = new DingdanDetail();
				detail.order_id = object.getString("order_id");
				detail.seller_id = object.getString("seller_id");
				detail.order_sn = object.getString("order_sn");
				detail.status = object.getString("status");
				detail.order_amount = object.getString("order_amount");
				detail.add_time = object.getString("add_time");
				detail.store_name = object.getString("store_name");
				detail.owner_name = object.getString("owner_name");
				detail.tel = object.getString("tel");
				detail.discount = object.getString("discount");
				detail.status_text = object.getString("status_text");

				JSONArray goods = object.getJSONArray("good_list");
				List<DingdanDetail_good_list> list = new ArrayList<DingdanDetail_good_list>();
				for (int i = 0; i < goods.size(); i++) {
					JSONObject jsonObject2 = goods.getJSONObject(i);
					DingdanDetail_good_list detail_good_list = new DingdanDetail_good_list();
					detail_good_list.goods_id = jsonObject2.getString("goods_id");
					detail_good_list.goods_name = jsonObject2.getString("goods_name");
					detail_good_list.price = jsonObject2.getString("price");
					detail_good_list.quantity = jsonObject2.getString("quantity");
					detail_good_list.goods_image = jsonObject2.getString("goods_image");

					List<Gouwuche_specification> specifications = new ArrayList<Gouwuche_specification>();
					JSONArray specificationArray = jsonObject2.getJSONArray("order_spec");
					if (specificationArray != null) {
						for (int k = 0; k < specificationArray.size(); k++) {
							JSONObject specificationObject = specificationArray.getJSONObject(k);
							Gouwuche_specification specification = new Gouwuche_specification();
							specification.spec_id = specificationObject.getString("spec_id");
							specification.spec_1 = specificationObject.getString("spec_1");
							specification.price = specificationObject.getFloatValue("price");
							specification.spec = JSON.parseArray(specificationObject.getString("spec"),
									Gouwuche_spec.class);
							specifications.add(specification);
						}
					}
					detail_good_list.specification = specifications;
					list.add(detail_good_list);
				}

				DingdanDetail_orderextm detail_orderextm = JSON.parseObject(object.getString("orderextm"),
						DingdanDetail_orderextm.class);
				detail.good_list = list;
				adapter.addAll(list);
				detail.orderextm = detail_orderextm;
				show(detail);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	

}
